# demo 
export ROOT_PATH=$PWD

CC_NAME_GET=`cat .config | grep CONFIG_LIBC=`
CC_NAME=${CC_NAME_GET:13:-1}
export KERNEL_DIR=$ROOT_PATH/build_dir/target-aarch64_generic_$CC_NAME/linux-layerscape_armv8_64b/linux-5.4.87/

if [ "$CC_NAME" = "glibc" ]; then
    export ROOTFS_DIR=$ROOT_PATH/bin/targets/layerscape/armv8_64b-glibc/
else
    export ROOTFS_DIR=$ROOT_PATH/bin/targets/layerscape/armv8_64b/
fi

export KERNEL_NAME=vmlinuz-dtb.img

BOARD_ROOTFS_NAME_GET=`cat .config | grep CONFIG_TARGET_PROFILE`
BOARD_ROOTFS_NAME=${BOARD_ROOTFS_NAME_GET:30:-1}

export ROOTFS_NAME=openwrt-layerscape-armv8_64b-$BOARD_ROOTFS_NAME-rootfs.tar.gz

function generate_itb()
{
    echo "now do the kernel package handle."
    cd $KERNEL_DIR
    rm -rf arch/arm64/boot/Image.gz
    cd arch/arm64/boot/ && cp Image Image_bak && gzip -9 Image && mv Image_bak Image

    cd $KERNEL_DIR
    mkimage -f linux_arm64.its vmlinuz-dtb.img #> /dev/null
    rm -rf arch/arm64-boot/Image.gz
    cd $ROOT_PATH
}

function generate_rootfs()
{
    echo "package kernel with rootfs."
    cd $ROOTFS_DIR
    mkdir -p tmp-rootfs

    cp $ROOTFS_NAME ${ROOTFS_NAME}-old
    tar zxvf $ROOTFS_NAME -C tmp-rootfs/ > /dev/null
    mv ${KERNEL_DIR}/${KERNEL_NAME} tmp-rootfs/ && cd tmp-rootfs/
    tar zcvf $ROOTFS_NAME * > /dev/null 
    mv $ROOTFS_NAME ../ && cd ../
    rm -rf tmp-rootfs    
    cd $ROOT_PATH
}

generate_itb
generate_rootfs






